<?php

namespace App\Http\Controllers\OpenShop\Feishu;

use App\Http\Controllers\BaseController;
use App\Http\Middleware\OpenShop\AppletAuth;
use App\Models\Wanggui\CustomerDepartmentFollowUpRecordModel;
use App\Service\OpenShop\FeishuMini\CustomerDepartmentService;
use App\Service\OpenShop\SiteLocation\AuthService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

class CustomerDepartmentController extends BaseController
{

    /**
     * 获得任务清单
     * @param Request $request
     * @return JsonResponse
     */
    public function site(Request $request): JsonResponse
    {
        $authUser = AuthService::getRmsUserInfo($request);
        $pageSize = (int)$request->get('pageSize', 10);
        $pageNum  = (int)$request->get('pageNum', 1);
        return $this->success(CustomerDepartmentService::site($pageSize, $pageNum, $authUser));
    }


    /**
     * 任务详情
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function detailSite(Request $request, $id): JsonResponse
    {
        // 不同路由获取用户id
        $action     = request()->route()->getAction();
        $middleware = attributeGet($action, "middleware", []);
        $authUser   = [];
        if (!in_array('kmsAuth:kmsApi', $middleware)) {
            $authUser = AuthService::getRmsUserInfo($request);
        }
        return $this->success(CustomerDepartmentService::detailSite($id, $authUser));
    }

    /**
     * 收藏任务
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function collectTask(Request $request, $id): JsonResponse
    {
        // 不同路由获取
        $action     = request()->route()->getAction();
        $middleware = attributeGet($action, "middleware", []);
        $authUser   = [];
        if (!in_array('kmsAuth:kmsApi', $middleware)) {
            $authUser = AuthService::getRmsUserInfo($request);
        }
        $collect  = (int)$request->post('collect');

        CustomerDepartmentService::collectTask($id, $collect, $authUser);

        return $this->success();
    }


    /**
     * 新增线索
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function addClue(Request $request): JsonResponse
    {
        $this->validate($request, [
            'status' => 'required|int',
            'id'     => 'int',
        ]);

        $id = (int)$request->post('id');
        if (!$id) {
            $this->validate($request, [
                'taskId'    => 'required|int',
                'preHaving' => 'required|string',
                'content'   => 'required|string',
            ]);
        }

        $authUser   = AuthService::getRmsUserInfo($request);
        $status     = (int)$request->post('status');
        $taskId     = (int)$request->post('taskId');
        $preHaving  = (string)$request->post('preHaving');
        $content    = (string)$request->post('content');
        $berthCount = (int)$request->post('berthCount', 0);

        return $this->success(CustomerDepartmentService::addClue($id, $taskId, $status, $preHaving, $content, $berthCount, $authUser));
    }


    /**
     * 线索列表
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function clue(Request $request): JsonResponse
    {

        $this->validate($request, [
            'taskId' => 'required|int'
        ]);

        $authUser = AuthService::getRmsUserInfo($request);
        $pageSize = (int)$request->get('pageSize', 10);
        $pageNum  = (int)$request->get('pageNum', 1);
        $taskId   = (int)$request->get('taskId');

        $searchText = (string)$request->get('searchText', '');
        return $this->success(CustomerDepartmentService::clue($pageSize, $pageNum, $searchText, $authUser, $taskId));
    }


    /**
     * 线索详情
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function detailClue(Request $request, $id): JsonResponse
    {
        $authUser = AuthService::getRmsUserInfo($request);
        return $this->success(CustomerDepartmentService::detailClue($id, $authUser));
    }

    /**
     * 添加客户
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function addDock(Request $request): JsonResponse
    {
        $this->validate($request, [
            'taskId' => 'required|int',
            'name'   => 'required|string',
        ]);

        $taskId        = (int)$request->post('taskId');
        $name          = $request->post('name');
        $phone         = (string)$request->post('phone', '');
        $id            = (int)$request->post('id', 0);
        $wechatCode    = $request->post('wechatCode', '');
        $gender        = (int)$request->post('gender', 0);
        $prefixAddress = $request->post('prefixAddress', '');
        $provinceCode  = (int)$request->post('provinceCode', 0);
        $cityCode      = (int)$request->post('cityCode', 0);
        $districtCode  = (int)$request->post('districtCode', 0);
        $company       = $request->post('company', '');
        $companyType   = (int)$request->post('companyType', 0);
        $type          = (int)$request->post('type', 0);
        $isOnTheJob    = (int)$request->post('isOnTheJob', 0);
        $position      = $request->post('position', '');
        $remark        = $request->post('remark', '');
        $authUser      = AuthService::getRmsUserInfo($request);
        if (!$phone && !$wechatCode) {
            $this->fail(403, '手机号和微信号请任选一项填写');
        }

        return $this->success(CustomerDepartmentService::addDock($id, $taskId, $name, $phone, $wechatCode, $gender, $prefixAddress, $provinceCode, $cityCode, $districtCode, $company, $companyType, $type, $isOnTheJob, $position, $remark, $authUser));
    }


    /**
     * 客户列表
     * @param Request $request
     * @return JsonResponse
     */
    public function dock(Request $request): JsonResponse
    {
        $authUser = AuthService::getRmsUserInfo($request);
        $pageSize = (int)$request->get('pageSize', 10);
        $pageNum  = (int)$request->get('pageNum', 1);

        // 搜索条件
        $search = (string)$request->get('search', '');

        if (is_numeric($search)) {
            $phone = $search;
            $name  = '';
        } else {
            $name  = $search;
            $phone = '';
        }

        return $this->success(CustomerDepartmentService::dock($pageSize, $pageNum, $name, $phone, $authUser));
    }


    /**
     * 客户详情
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function detailDock(Request $request, $id): JsonResponse
    {
        $authUser = AuthService::getRmsUserInfo($request);
        return $this->success(CustomerDepartmentService::detailDock($id, $authUser));
    }


    /**
     * 新增跟进记录
     * @param Request $request
     * @return JsonResponse
     * @throws ValidationException
     */
    public function addFollowUp(Request $request): JsonResponse
    {
        $this->validate($request, [
            'taskId'  => 'required|int',
            'type'    => 'required|int',
            'method'  => 'required|int',
            'files'   => 'required|array',
            'content' => 'required|string',
        ]);

        $authUser  = AuthService::getRmsUserInfo($request);
        $type      = (int)$request->post('type');
        $taskId    = (int)$request->post('taskId');
        $method    = (int)$request->post('method');
        $files     = $request->post('files');
        $content   = $request->post('content');
        $kpPartyId = (int)$request->post('kpPartyId', 0);
        $clueId    = (int)$request->post('clueId', 0);

        if ($type == CustomerDepartmentFollowUpRecordModel::TYPE_PARTY_A && $kpPartyId == 0) {
            return $this->fail(403, '跟进甲方时跟进客户为必填');
        }

        CustomerDepartmentService::addFollowUp($taskId, $type, $method, $files, $content, $kpPartyId, $clueId, $authUser);

        return $this->success();
    }


    /**
     * 获得跟进记录
     * @param Request $request
     * @return JsonResponse
     */
    public function followUp(Request $request): JsonResponse
    {
        $authUser = AuthService::getRmsUserInfo($request);
        $pageSize = (int)$request->get('pageSize', 10);
        $pageNum  = (int)$request->get('pageNum', 1);
        return $this->success(CustomerDepartmentService::followUp($pageSize, $pageNum, $authUser));
    }


    /**
     * 跟进记录详情
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function detailFollowUp(Request $request, $id): JsonResponse
    {
        $authUser = AuthService::getRmsUserInfo($request);
        return $this->success(CustomerDepartmentService::detailFollowUp($id, $authUser));
    }

}
